package com.mine.yyb.modules.sys.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.jdbc.MineBaseDao;
import com.mine.yyb.common.utils.jdbc.MinePagination;
import com.mine.yyb.modules.sys.dao.IUserDao;
import com.mine.yyb.modules.sys.enums.UserRoleEnum;
import com.mine.yyb.modules.sys.enums.UserStatusEnum;
import com.mine.yyb.modules.sys.vo.UserVO;

@Repository
public class UserDaoImpl extends MineBaseDao implements IUserDao
{
    private static Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
    
    @Override
    public int save(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuffer sql = new StringBuffer();
            
            sql.append(" insert into " + UserVO.TABLE_NAME);
            sql.append(" (username,password,innerID,parentInnerID,email,qq,status,createDate)");
            sql.append(" values(:username,:password,:innerID,:parentInnerID,:email,:qq,:status,now())");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), userVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int update(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + UserVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            if (StringUtils.isNotBlank(userVO.getUsername()))
            {
                sql.append(" ,username = :username");
            }
            if (StringUtils.isNotBlank(userVO.getPassword()))
            {
                sql.append(" ,password = :password");
            }
            if (StringUtils.isNotBlank(userVO.getEmail()))
            {
                sql.append(" ,email=:email");
            }
            if (userVO.getStatus() != 0)
            {
                sql.append(" ,status=:status");
            }
            sql.append(" where 1 = 1");
            if (StringUtils.isNotBlank(userVO.getInnerID()))
            {
                sql.append(" and innerID = :innerID");
            }
            if (StringUtils.isNotBlank(userVO.getEmail()))
            {
                sql.append(" and email = :email");
            }
            logger.info(sql.toString());
            
            return super.update(sql.toString(), userVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int delete(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append("delete from " + UserVO.TABLE_NAME);
            sql.append(" where innerID = :innerID");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), userVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int updateUserPay(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + UserVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            sql.append(" ,payAccount=:payAccount");
            sql.append(" ,name=:name");
            sql.append(" where innerID = :innerID");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), userVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public int updateUserQQ(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuilder sql = new StringBuilder();
            sql.append(" update " + UserVO.TABLE_NAME);
            sql.append(" set updateDate = now()");
            sql.append(" ,qq=:qq");
            sql.append(" where innerID = :innerID");
            
            logger.info(sql.toString());
            
            return super.update(sql.toString(), userVO);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public UserVO queryUser(UserVO userVO)
        throws MineException
    {
        
        try
        {
            logger.info(userVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append(
                "select username, password, innerID, parentInnerID, email, status, createDate, payAccount, qq, name from "
                    + UserVO.TABLE_NAME);
            sql.append(" where 1 = 1");
            
            if (StringUtils.isNotBlank(userVO.getUsername()))
            {
                sql.append(" and username = :username");
            }
            if (StringUtils.isNotBlank(userVO.getPassword()))
            {
                sql.append(" and password = :password");
            }
            if (StringUtils.isNotBlank(userVO.getEmail()))
            {
                sql.append(" and email = :email");
            }
            if (StringUtils.isNotBlank(userVO.getInnerID()))
            {
                sql.append(" and innerID = :innerID");
            }
            if (userVO.getStatus() != 0)
            {
                sql.append(" and status = :status");
            }
            if(StringUtils.isNotBlank(userVO.getPayAccount())) 
            {
                sql.append(" and payAccount = :payAccount");
            }
            if (userVO.getId() != 0)
            {
                sql.append(" and id = :id");
            }
            sql.append(" and role = :role");
            
            logger.info(sql.toString());
            return super.queryForObject(sql.toString(), userVO, UserVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public MinePagination<UserVO> queryUser(UserVO userVO, int sEcho, int iDisplayStart, int iDisplayLength)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString() + ", iDisplayStart=" + iDisplayStart + ", iDisplayLength=" + iDisplayLength);
            
            StringBuilder sql = new StringBuilder();
            sql.append("select a.id, a.username, a.innerID, ");
            sql.append("a.role, a.email, a.status, a.createDate, a.payAccount, a.qq ");
            sql.append("from " + UserVO.TABLE_NAME + " a ");
            sql.append("where 1=1 ");
            if (StringUtils.isNotBlank(userVO.getCreateDate()))
            {
                sql.append("and a.createDate like '" + userVO.getCreateDate() + "%' ");
            }
            if (StringUtils.isNotBlank(userVO.getUsername()))
            {
                sql.append("and a.username = :username ");
            }
            if (StringUtils.isNotBlank(userVO.getEmail()))
            {
                sql.append("and a.email = :email ");
            }
            sql.append("order by a.updateDate desc");
            
            logger.info("sql:{}", sql.toString());
            return super.queryForList(sql.toString(), userVO, UserVO.class, sEcho, iDisplayStart, iDisplayLength);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
    @Override
    public List<UserVO> queryChildList(UserVO userVO)
        throws MineException
    {
        try
        {
            logger.info(userVO.toString());
            
            StringBuffer sql = new StringBuffer();
            sql.append("select a.id, a.username, a.innerID, a.parentInnerID, a.payAccount, a.qq, a.name, a.createDate ");
            sql.append("from " + UserVO.TABLE_NAME + " a ");
            sql.append("where 1 = 1 ");
            sql.append("and a.role = " + UserRoleEnum.USER_NORMAL_ROLE.getRole() + " ");
            sql.append("and a.status = " + UserStatusEnum.USER_ACTIVATED_STATUS.getStatus() + " ");
            sql.append("and a.innerID in "); 
            sql.append("(select parentInnerID from " + UserVO.TABLE_NAME + " where parentInnerID is not null ");
            sql.append("union all ");
            sql.append("select innerID from " + UserVO.TABLE_NAME + " where parentInnerID is not null)");
            
            logger.info("sql:{}", sql.toString());
            
            return super.queryForList(sql.toString(), userVO, UserVO.class);
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_DB_EXECUTE_FAIL, e);
        }
    }
    
}
